package grpc

import (
	"context"
	"ctrip-travel/rpc/models/dao"
	"ctrip-travel/rpc/models/services"
	"ctrip-travel/rpc/pkg"
	user1 "ctrip-travel/rpc/proto/user"
	"github.com/dgrijalva/jwt-go"
	"github.com/google/uuid"
	"time"
)

type UserServer struct {
	user1.UnimplementedRegisterServerServer
}

func (s *UserServer) Register(ctx context.Context, req *user1.RegisterReq) (*user1.RegisterResp, error) {
	user := dao.User{
		Name: req.Name,
		Pass: req.Pass,
	}
	err := services.CreateUser(user)
	if err != nil {
		return &user1.RegisterResp{
			Code: 400,
			Msg:  "创建失败",
		}, err
	}
	resp := &user1.RegisterResp{
		Code: 200,
		Msg:  "创建成功",
	}
	return resp, err
}

func (s *UserServer) Login(ctx context.Context, req *user1.LoginReq) (*user1.LoginResp, error) {
	res, err := services.GetUserInfo(req.Name)
	if err != nil {
		return &user1.LoginResp{
			Code: 400,
			Msg:  "用户不存在",
			Data: "nil",
		}, err
	}
	CustomClaims := pkg.CustomClaims{
		ID: res.ID,
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: time.Now().Unix() + 86400,
		},
	}
	token, err := pkg.NewJWT("2211A").CreateToken(CustomClaims)
	if err != nil {
		return &user1.LoginResp{
			Code: 400,
			Msg:  "登录失败",
			Data: "nil",
		}, err
	}
	resp := &user1.LoginResp{
		Code: 200,
		Msg:  "登录成功",
		Data: token,
	}
	return resp, err
}

func (s *UserServer) AddVote(ctx context.Context, req *user1.AddVoteReq) (*user1.AddVoteResp, error) {
	vote := dao.Vote{
		Ticket: req.Ticket,
		State:  int(req.State),
		Num:    int(req.Num),
		Seat:   req.Seat,
		Price:  float64(req.Price),
	}
	err := services.CreateVote(vote)
	if err != nil {
		return &user1.AddVoteResp{
			Code: 400,
			Msg:  "添加失败",
		}, err
	}
	resp := &user1.AddVoteResp{
		Code: 200,
		Msg:  "添加成功",
	}
	return resp, err
}

func (s *UserServer) AddOrder(ctx context.Context, req *user1.AddOrderReq) (*user1.AddOrderResp, error) {
	orderStr := uuid.ClockSequence()
	order := dao.Order{
		OrderId: orderStr,
		UserId:  int(req.UserId),
		VoteId:  int(req.VoteId),
		Money:   float64(req.Money),
	}
	err := services.CreateOrder(order)
	if err != nil {
		return &user1.AddOrderResp{
			Code: 400,
			Msg:  "创建订单失败",
		}, err
	}
	resp := &user1.AddOrderResp{
		Code: 200,
		Msg:  "创建订单成功",
	}
	return resp, err
}

func (s *UserServer) QueryVote(ctx context.Context, req *user1.QueryVoteReq) (*user1.QueryVoteResp, error) {
	vote, err := services.QueryVote(int(req.State))
	if err != nil {
		return &user1.QueryVoteResp{
			Code: 400,
			Msg:  "查询失败",
			Data: nil,
		}, err
	}
	var voteList []*user1.VoteInfo
	for _, v := range vote {
		voteList = append(voteList, &user1.VoteInfo{
			Ticket: v.Ticket,
			State:  int64(v.State),
			Num:    int64(v.Num),
			Seat:   v.Seat,
			Price:  float32(v.Price),
		})
	}
	resp := &user1.QueryVoteResp{
		Code: 200,
		Msg:  "查询成功",
		Data: voteList,
	}
	return resp, err
}

func (s *UserServer) AddBuyTickets(ctx context.Context, req *user1.AddBuyTicketsReq) (*user1.AddBuyTicketsResp, error) {
	Tickets := dao.BuyTickets{
		UserId: int(req.UserId),
		VoteId: int(req.VoteId),
		Ticket: req.Ticket,
		Money:  float64(req.Money),
		Seat:   req.Seat,
	}
	err := services.CreateBuyTickets(Tickets)
	if err != nil {
		return &user1.AddBuyTicketsResp{
			Code: 400,
			Msg:  "创建失败",
		}, err
	}
	resp := &user1.AddBuyTicketsResp{
		Code: 200,
		Msg:  "成功",
	}
	return resp, err
}

func (s *UserServer) OrderList(ctx context.Context, req *user1.OrderListReq) (*user1.OrderListResp, error) {
	list, err := services.OrderList(int(req.UserId))
	if err != nil {
		return &user1.OrderListResp{
			Code: 400,
			Msg:  "详情展示失败",
			Data: nil,
		}, err
	}
	var orderList []*user1.OrderInfo
	for _, v := range list {
		orderList = append(orderList, &user1.OrderInfo{
			UserId: int64(v.UserId),
			VoteId: int64(v.VoteId),
			Money:  float32(v.Money),
		})
	}
	resp := &user1.OrderListResp{
		Code: 200,
		Msg:  "详情展示成功",
		Data: orderList,
	}
	return resp, err
}
